package com.shengyijie.adapter.viewadapter;

import java.util.List;
 
import com.shengyijie.activity.R;
import com.shengyijie.activity.commen.ItemListActivity;
import com.shengyijie.adapter.asyncimageadapter.AsyncImageLoader;
import com.shengyijie.adapter.asyncimageadapter.AsyncImageLoader.ImageCallback;
import com.shengyijie.adapter.viewcache.CategoryListViewCache; 
import com.shengyijie.adapter.viewcache.ItemListViewCache;
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.object.baseobject.Category;
import com.shengyijie.model.object.baseobject.Item;
import com.shengyijie.util.Utility;


import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter; 
import android.widget.ImageView;
import android.widget.ListView; 
import android.widget.TextView;
import android.widget.Toast;


public class ItemListAdapter extends ArrayAdapter<Item>{
 
	private Activity activity;
	private AsyncImageLoader asyncImageLoader;
	private ListView listView;
	
	public ItemListAdapter(Activity activity, List<Item> newsList,ListView listView) {
		  super(activity, 0, newsList);
		  this.activity=activity;  
		  asyncImageLoader = new AsyncImageLoader();
		  this.listView=listView;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		ItemListViewCache viewCache;
		try{ 
				if (convertView == null) {
					LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater(); 
					convertView = inflater.inflate(R.layout.itemlistcell, null);  
					viewCache = new ItemListViewCache(convertView);
					convertView.setTag(viewCache);
				}else{
					viewCache = (ItemListViewCache) convertView.getTag(); 
				}  
				try{ 
					ImageView imageView = viewCache.getImage();  
					String imageUrl=getItem(position).getImageUrl();
					imageView.setTag(imageUrl);
					Drawable cachedImage = null;
					
					cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
						public void imageLoaded(Drawable imageDrawable, String imageUrl) {
							ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
							if (imageViewByTag != null) {
								imageViewByTag.setImageDrawable(imageDrawable); 
							}
						}
					});
					
					if (cachedImage == null) {
						imageView.setImageResource(R.drawable.load);
					}else{
						imageView.setImageDrawable(cachedImage);
						cachedImage=null;
					}
  
					TextView name = viewCache.getName();   
					name.setText(getItem(position).getName());
//					convertView.setOnTouchListener(new OnTouchListener(){
//
//						@Override
//						public boolean onTouch(View view, MotionEvent event) {
//							    if(event.getAction()==MotionEvent.ACTION_DOWN){ 
//							    	TextView s=(TextView) view.findViewById(R.id.name);
//							    	s.setTextColor(0xff6750f3); 
//							    	return false;
//							    }  
//							    if(event.getAction()!=MotionEvent.ACTION_DOWN){ 
//							    	TextView s=(TextView) view.findViewById(R.id.name);
//							    	s.setTextColor(0xff353535); 
//							    	return false;
//							    }  
//				                
////				                if(event.getAction()==MotionEvent.ACTION_UP){  
////				                	TextView s=(TextView) view.findViewById(R.id.name);
////							    	s.setTextColor(0xff353535); 
////							    	return true;
////				                } 
////				                
////				                 
////				                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){  
////				                	TextView s=(TextView) view.findViewById(R.id.name);
////							    	s.setTextColor(0xff353535); 
////							    	return true;
////				                } 
//				                return false;
//						}});
					
					TextView des = viewCache.getDes();   
					des.setText(getItem(position).getDes());
					
				}catch(Exception e){}
		}catch(Exception e){}
		return convertView;
	}
}
